package glaf;

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import utils.GColor;
import utils.GIcon;

@SuppressWarnings("serial")
public class GFrame extends JFrame {

	private static Point point = new Point();
	private GButton btMax;
	private GButton btMin;
	private GButton btClose;

	public GFrame(String title) {
		this(title, null);
	}

	public GFrame(ImageIcon icon) {
		this(null, icon);
	}

	public GFrame(String title, ImageIcon icon) {
		this();
		addHeaderBar(title, icon);
	}

	public GFrame() {
		setSize(600, 500);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setUndecorated(true);
		getContentPane().setBackground(GColor.BLUE);
		setIconImage(GIcon.ICON_FLORIST48.getImage());

		addMouseListener(new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent e) {
				point.x = e.getX();
				point.y = e.getY();
			}
		});
		addMouseMotionListener(new MouseMotionAdapter() {

			@Override
			public void mouseDragged(MouseEvent e) {
				Point p = getLocation();
				setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
			}
		});

	}

	private void addHeaderBar(String title, ImageIcon icon) {
		btClose = new GButton(GIcon.BT_CLOSE36);
		btClose.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		btMax = new GButton(GIcon.BT_MAX36);
		btMax.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (getExtendedState() == JFrame.NORMAL) {
					btMax.setIcon(GIcon.BT_MAX_EXIT36);
					setExtendedState(JFrame.MAXIMIZED_BOTH);
				} else {
					btMax.setIcon(GIcon.BT_MAX36);
					setExtendedState(JFrame.NORMAL);
				}
			}
		});
		btMin = new GButton(GIcon.BT_MIN36);
		btMin.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				setExtendedState(JFrame.ICONIFIED);
				btMax.setIcon(GIcon.BT_MAX36);
			}
		});

		JPanel pnNorth = new JPanel();
		pnNorth.setLayout(new BoxLayout(pnNorth, BoxLayout.X_AXIS));
		pnNorth.setBackground(GColor.BLUE700);
		pnNorth.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
		pnNorth.add(new GButton(icon));
		pnNorth.add(new GLabel(title, 20));
		pnNorth.add(Box.createHorizontalGlue());
		pnNorth.add(btMin);
		pnNorth.add(btMax);
		pnNorth.add(btClose);
		add(pnNorth, BorderLayout.NORTH);
	}

}